package com.wfcm.resolver;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.wfcm.annotation.LoginUser;
import com.wfcm.entity.WfMemberEntity;
import com.wfcm.interceptor.SignCheckInterceptor;
import com.wfcm.service.WfMemberService;
/**
 * 有@LoginUser注解的方法参数，注入当前登录用户
 * @email 
 * @date 2018-01-22 22:02
 */
public class LoginUserHandlerMethodArgumentResolver  implements HandlerMethodArgumentResolver {

	private  WfMemberService wfMemberService;

    public void setUserService(WfMemberService wfMemberService) {
        this.wfMemberService = wfMemberService;
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(WfMemberEntity.class) && parameter.hasParameterAnnotation(LoginUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
        //获取用户ID
        Object object = request.getAttribute(SignCheckInterceptor.LOGIN_USER_KEY, RequestAttributes.SCOPE_REQUEST);
        if(object == null){
            return null;
        }
        //获取用户信息
        WfMemberEntity member = wfMemberService.queryObject((int)object);
        return member;
    }
}
